import java.io.IOException;
import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.Sprite;
public class MainCanvas extends Canvas implements CommandListener {
    private Command out = new Command("Dang xuat", Command.EXIT, 0);
    String[] menu;
    int selected;
    private String title = new String("KIWI SOCIAL NETWORK");
    Image[] iconArray;
    Image img;
   
    int top = 30;
    int x = 9;
    int a = 3;
    int b = 3;
    int width = (this.getWidth()-x*2)/a;
    int height = (this.getHeight()-x)/b;
    
    
    public MainCanvas() throws IOException{
        menu = new String[]{"Chat Kiwi", "Tien ich", "Thong bao", "Tro giup"};
        
        iconArray = new Image[4];
        this.addCommand(out);
        this.setCommandListener(this);
        
            iconArray[0] = Image.createImage("/chat.png");
            iconArray[1] = Image.createImage("/utin.png");
            iconArray[2] = Image.createImage("/noti.png");
            iconArray[3] = Image.createImage("/help.png");
            img = Image.createImage("/walls.png");
            
        


    }
    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(0x7bce28);
        
        g.fillRect(x+(selected%3)*width, (top)+(selected/3)*(height)-(top/3), width, height);
        g.drawImage(img, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
        g.setColor(0xff0000);
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        g.setFont(f);
        g.drawString(title, getWidth() / 2, 2, Graphics.TOP | Graphics.HCENTER);
        
        
        g.setColor(0x000000);
        
        
        
        
        
        


        for (int i=0; i<menu.length;i++){

            g.drawRegion(iconArray[i], 0, 0, 50,50, Sprite.TRANS_NONE, x+(i%a)*width+width/2, top+(i/3)*height+width/2, Graphics.VCENTER | Graphics.HCENTER);
           
           
            


            g.drawString(menu[i], x+(i%3)*width+width/2, top+(i/3)*height+width+a+b, Graphics.BOTTOM | Graphics.HCENTER);
        }
        
        

    }

   protected void keyPressed(int keyCode)

  {
       int gameAction = getGameAction(keyCode);
       
        
       
        switch (gameAction){
        case RIGHT:

            selected = (byte) (++selected%menu.length);
            repaint();
        }
        switch (gameAction){
        case LEFT:
            selected = (--selected)%menu.length;
            if (selected==-1) selected = menu.length-1;
            repaint();
        }
        switch (gameAction){
        case UP:
            selected -= a;
            if (selected<0) selected = ((menu.length-1)/3)*a+(selected+a);
            if (selected >= menu.length) selected = menu.length-1;
            repaint();
        }
        switch (gameAction){
        case DOWN:
            selected += a;
            if (selected>=menu.length)
                if (selected/3 == (menu.length-1)/3) selected = menu.length-1;
                else selected = selected%3;
            repaint();
        }
        switch (gameAction) {
            case FIRE:
            onSelect(selected);
        }
   }
   
   public void onSelect(int index) {
        if (index == 0) {
           
            SetDisplay.setCurrent(SetDisplay.FRIENDSLIST);
        
        }
        else if (index == 1) {
            //
        }
        else if (index == 2) {
            //
        }
        else if (index == 3) {
            //
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == out) {
            SetDisplay.setCurrent(SetDisplay.SIGNIN);
        }
    }
   
   
   
   
   
   
   
   
   
   
   
   



}